import axios from 'axios';
// import { MessageBox, Message } from 'element-ui'
// import store from '@/store'
import { getToken } from '@/util/auth'

const serviceUpload = axios.create({
  baseURL: process.env.VUE_APP_BASE_API_FILES, // url = base url + request url VUE_APP_BASE_API_purchase
  timeout: 5000 // request timeout
})

const testUrl = ['vue-element-admin']

// 请求拦截器
serviceUpload.interceptors.request.use(
  config => {
    var arr = config.url.split('/')
    var whiteList = false
    testUrl.forEach(item => {
      if (arr.indexOf(item) !== -1) {
        whiteList = true
        return
      }
    })
    if (whiteList) {
      config.baseURL = process.env.VUE_APP_BASE_API
    } else {
      config.baseURL = process.env.VUE_APP_BASE_API_FILES
    }
    // if (store.getters.token) {
    //   config.headers['Authorization'] = getToken()
    // }
    return config
  },
  error => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// 响应拦截器
serviceUpload.interceptors.response.use(
  response => {
    const res = response.data
    // if the custom status is not 20000, it is judged as an error.
    if (res.status !== 200 && res.code !== 20000) {
      Message({
        message: res.message || res.errorMessage || 'Error',
        type: 'error',
        duration: 5 * 1000
      })

      // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
      // if (res.status === 50008 || res.status === 50012 || res.status === 50014) {
      //   // to re-login
      //   MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
      //     confirmButtonText: 'Re-Login',
      //     cancelButtonText: 'Cancel',
      //     type: 'warning'
      //   }).then(() => {
      //     store.dispatch('user/resetToken').then(() => {
      //       location.reload()
      //     })
      //   })
      // }
      if (res.status === 10000) {
		  console.log('编号重复，请稍后再试')
        // Message({
        //   message: '编号重复，请稍后再试',
        //   type: 'error',
        //   duration: 3 * 1000
        // })
        return Promise.reject({ code: 1 })
      }
      if (res.status === 401) {
		  console.log('账号已下线,请重新登录,3秒后自动跳转')
        store.dispatch('user/logout')
        // Message({
        //   message: '账号已下线,请重新登录,3秒后自动跳转',
        //   type: 'error',
        //   duration: 3 * 1000
        // })
        setTimeout(() => {
          window.location.pathname = '/login'
        }, 3000)
      } else {
		  console.log('账号已下线,请重新登录,3秒后自动跳转')
        // Message({
        //   message: res.message || res.errorMessage || 'Error',
        //   type: 'error',
        //   duration: 5 * 1000
        // })
      }
      return Promise.reject(new Error(res.message || 'Error'))
    } else {
      return res
    }
  },
  error => {
    if (error.code === 'ECONNABORTED') {
		console.log('请求超时，请稍后操作')
      // Message({
      //   message: '请求超时，请稍后操作',
      //   type: 'error',
      //   duration: 5 * 1000
      // })
    } else {
      // Message({
      //   message: error.message,
      //   type: 'error',
      //   duration: 5 * 1000
      // })
    }
    return Promise.reject(error)
  }
)

export default serviceUpload
